#include "bottlingplant.h"
#include <iostream>
using namespace std;

// initializing
BottlingPlant::BottlingPlant( Printer &prt, NameServer &nameServer, unsigned int numVendingMachines, unsigned int maxShippedPerFlavour, unsigned int maxStockPerFlavour, unsigned int timeBetweenShipments ) {
  prt.print(Printer::BottlingPlant, 'S');
  this->truck = new Truck(prt,nameServer, *this, numVendingMachines, maxStockPerFlavour);
  this->nameServer = &nameServer;
  this->numVendingMachines = numVendingMachines;
  this->maxShippedPerFlavour = maxShippedPerFlavour;
  this->maxStockPerFlavour = maxStockPerFlavour;
  this->timeBetweenShipments = timeBetweenShipments;
  this->prt = &prt;
  prt.print(Printer::BottlingPlant, 'G', productionline());
  truck->action();
}

// destructor
BottlingPlant::~BottlingPlant() {
    prt->print(Printer::BottlingPlant, 'F');
  this->nameServer = NULL;
  this->numVendingMachines = 0;
  this->maxShippedPerFlavour = 0;
  this->maxStockPerFlavour = 0;
    this->timeBetweenShipments = 0;
    delete this->truck;
}

void BottlingPlant::getShipment( unsigned int cargo[] ) {
  prt->print(Printer::BottlingPlant, 'P');
  for(int i = 0; i < 4; i++) {
    cargo[i] = numStock[i];
    numStock[i] = 0;
  }
}



void BottlingPlant::action() {
  if (prng(4) == 0) {
    prt->print(Printer::BottlingPlant, 'X');
    return;
  } else {
    if (prng(timeBetweenShipments-1) == 0) {
      unsigned int total = productionline();
      prt->print(Printer::BottlingPlant, 'G', total);
      truck->action();
    }
    else {
      return;
    }
  }
}

unsigned int BottlingPlant::productionline() {
  unsigned int total = 0;
  for(int i = 0; i < 4; i++) {
    numStock[i] = prng(maxShippedPerFlavour);
    total += numStock[i];
  }
  return total;
}